<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>docker的使用 | 欢迎来到guo的博客!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一、Docker介绍1、Docker介绍Docker 是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中，然后发布到任何流行的 Linux或Windows 机器上，也可以实现虚拟化。容器是完全使用沙箱机制，相互之间不会有任何接口。 一个完整的Docker有以下几个部分组成：  DockerClient客户端    –客户端 Docker Daemon守护进程  –服">
<meta property="og:type" content="article">
<meta property="og:title" content="docker的使用">
<meta property="og:url" content="http://shangguan-lingfeng.gitee.io/blog1/2021/04/23/docker%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="欢迎来到guo的博客!">
<meta property="og:description" content="一、Docker介绍1、Docker介绍Docker 是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中，然后发布到任何流行的 Linux或Windows 机器上，也可以实现虚拟化。容器是完全使用沙箱机制，相互之间不会有任何接口。 一个完整的Docker有以下几个部分组成：  DockerClient客户端    –客户端 Docker Daemon守护进程  –服">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://docs.docker.com/engine/images/architecture.svg">
<meta property="article:published_time" content="2021-04-22T16:00:00.000Z">
<meta property="article:modified_time" content="2021-04-23T13:22:25.683Z">
<meta property="article:author" content="Jerry">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://docs.docker.com/engine/images/architecture.svg">
  
    <link rel="alternate" href="/blog1/atom.xml" title="欢迎来到guo的博客!" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog1/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog1/css/style.css">

  
    
<link rel="stylesheet" href="/blog1/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog1/" id="logo">欢迎来到guo的博客!</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog1/">Home</a>
        
          <a class="main-nav-link" href="/blog1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://shangguan-lingfeng.gitee.io/blog1"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-docker的使用" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/04/23/docker%E7%9A%84%E4%BD%BF%E7%94%A8/" class="article-date">
  <time class="dt-published" datetime="2021-04-22T16:00:00.000Z" itemprop="datePublished">2021-04-23</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      docker的使用
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="一、Docker介绍"><a href="#一、Docker介绍" class="headerlink" title="一、Docker介绍"></a>一、Docker介绍</h1><h2 id="1、Docker介绍"><a href="#1、Docker介绍" class="headerlink" title="1、Docker介绍"></a>1、Docker介绍</h2><p>Docker 是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E9%95%9C%E5%83%8F/1574">镜像</a>中，然后发布到任何流行的 <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/Linux">Linux</a>或<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/Windows/165458">Windows</a> 机器上，也可以实现<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E8%99%9A%E6%8B%9F%E5%8C%96/547949">虚拟化</a>。容器是完全使用<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%B2%99%E7%AE%B1/393318">沙箱</a>机制，相互之间不会有任何接口。</p>
<p>一个完整的Docker有以下几个部分组成：</p>
<ol>
<li>DockerClient客户端    –客户端</li>
<li>Docker Daemon守护进程  –服务端</li>
<li>Docker Image镜像</li>
<li>DockerContainer容器 </li>
</ol>
<h2 id="Docker架构"><a href="#Docker架构" class="headerlink" title="Docker架构"></a>Docker架构</h2><p>Docker使用客户端-服务器架构。Docker<em>客户端</em>与Docker<em>守护进程</em>进行对话，该<em>守护进程</em>完成了构建，运行和分发Docker容器的繁重工作。Docker客户端和守护程序<em>可以</em> 在同一系统上运行，或者您可以将Docker客户端连接到远程Docker守护程序。Docker客户端和守护程序在UNIX套接字或网络接口上使用REST API进行通信。另一个Docker客户端是Docker Compose，它使您可以处理由一组容器组成的应用程序。</p>
<p><img src="https://docs.docker.com/engine/images/architecture.svg" alt="Docker架构图"></p>
<h3 id="Docker守护程序"><a href="#Docker守护程序" class="headerlink" title="Docker守护程序"></a>Docker守护程序</h3><p>Docker守护程序（<code>dockerd</code>）侦听Docker API请求并管理Docker对象，例如图像，容器，网络和卷。守护程序还可以与其他守护程序通信以管理Docker服务。</p>
<h3 id="Docker客户端"><a href="#Docker客户端" class="headerlink" title="Docker客户端"></a>Docker客户端</h3><p>Docker客户端（<code>docker</code>）是许多Docker用户与Docker交互的主要方式。当您使用诸如之类的命令时<code>docker run</code>，客户端会将这些命令发送到<code>dockerd</code>，以执行这些命令。该<code>docker</code>命令使用Docker API。Docker客户端可以与多个守护程序通信。</p>
<h3 id="Docker注册表"><a href="#Docker注册表" class="headerlink" title="Docker注册表"></a>Docker注册表</h3><p>Docker<em>注册表</em>存储Docker映像。Docker Hub是任何人都可以使用的公共注册表，并且默认情况下，Docker已配置为在Docker Hub上查找映像。您甚至可以运行自己的私人注册表。</p>
<p>使用<code>docker pull</code>或<code>docker run</code>命令时，将从配置的注册表中提取所需的图像。使用该<code>docker push</code>命令时，会将映像推送到已配置的注册表。</p>
<h3 id="Docker对象"><a href="#Docker对象" class="headerlink" title="Docker对象"></a>Docker对象</h3><p>使用Docker时，您正在创建和使用映像，容器，网络，卷，插件和其他对象。本节是其中一些对象的简要概述。</p>
<h1 id="二、Docker安装"><a href="#二、Docker安装" class="headerlink" title="二、Docker安装"></a>二、Docker安装</h1><h2 id="1、安装"><a href="#1、安装" class="headerlink" title="1、安装"></a>1、安装</h2><p>在新主机上首次安装Docker Engine之前，需要设置Docker存储库。之后，您可以从存储库安装和更新Docker。</p>
<h3 id="1-1-准备工具"><a href="#1-1-准备工具" class="headerlink" title="1.1 准备工具"></a>1.1 准备工具</h3><p>先安装yum工具命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y yum-utils</span><br></pre></td></tr></table></figure>


<p>然后 添加docker的仓库 repo</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yum-config-manager \</span><br><span class="line">    --add-repo \</span><br><span class="line">    https:&#x2F;&#x2F;download.docker.com&#x2F;linux&#x2F;centos&#x2F;docker-ce.repo</span><br></pre></td></tr></table></figure>


<h3 id="1-2-安装docker引擎"><a href="#1-2-安装docker引擎" class="headerlink" title="1.2 安装docker引擎"></a>1.2 安装docker引擎</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install docker-ce docker-ce-cli containerd.io</span><br></pre></td></tr></table></figure>




<h3 id="1-3-启动Docker服务"><a href="#1-3-启动Docker服务" class="headerlink" title="1.3 启动Docker服务"></a>1.3 启动Docker服务</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker</span><br></pre></td></tr></table></figure>


<h3 id="1-4-测试docker"><a href="#1-4-测试docker" class="headerlink" title="1.4 测试docker"></a>1.4 测试docker</h3><p>从Docker Hub网站上 拉取一个 hello-world镜像 然后运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>


<h1 id="三、镜像Image"><a href="#三、镜像Image" class="headerlink" title="三、镜像Image"></a>三、镜像Image</h1><p>镜像就类似于一个软件，我们运行就创建了一个容器。</p>
<p>基本命令格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker 命令</span><br></pre></td></tr></table></figure>
<h2 id="3-1-基础命令"><a href="#3-1-基础命令" class="headerlink" title="3.1 基础命令"></a>3.1 基础命令</h2><p>查看本地镜像列表</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images ls</span><br></pre></td></tr></table></figure>


<h3 id="1、查看本地镜像"><a href="#1、查看本地镜像" class="headerlink" title="1、查看本地镜像"></a>1、查看本地镜像</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# docker images</span><br><span class="line"></span><br><span class="line">REPOSITORY                 TAG            IMAGE ID       CREATED         SIZE</span><br><span class="line"></span><br><span class="line">nginx                      latest         08393e824c32   8 months ago    132MB</span><br><span class="line">mysql                      5.6            1d3d3845393d   8 months ago    302MB</span><br><span class="line">mysql                      latest         0d64f46acfd1   8 months ago    544MB</span><br><span class="line">centos                     7              b5b4d78bc90c   11 months ago   203MB</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>解释：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">REPOSITORY：表示镜像的仓库源</span><br><span class="line"></span><br><span class="line">TAG：镜像的标签</span><br><span class="line"></span><br><span class="line">IMAGE ID：镜像ID</span><br><span class="line"></span><br><span class="line">CREATED：镜像创建时间</span><br><span class="line"></span><br><span class="line">SIZE：镜像大小</span><br><span class="line">同一仓库源可以有多个 TAG，代表这个仓库源的不同个版本，如 ubuntu 仓库源里，有 15.10、14.04 等多个不同的版本，我们使用 REPOSITORY:TAG 来定义不同的镜像。</span><br></pre></td></tr></table></figure>


<h3 id="2、拉取镜像"><a href="#2、拉取镜像" class="headerlink" title="2、拉取镜像"></a>2、拉取镜像</h3><p>我们的镜像都可以存放到Docker Hub网站(gitee码云)上，这是一个开源的镜像库</p>
<p>如果从这个网站上 下载东西 就可以使用 pull 拉取  </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">docker pull </span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 下载mysql,下面两条语句都可以</span></span><br><span class="line">docker image pull mysql:5.7</span><br><span class="line"></span><br><span class="line">docker pull mysql:5.7</span><br></pre></td></tr></table></figure>
<p>如果没有添加版本tag 则下载默认的最新版本 mysql:latest</p>
<h3 id="3、删除镜像"><a href="#3、删除镜像" class="headerlink" title="3、删除镜像"></a>3、删除镜像</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker rmi 镜像名</span><br></pre></td></tr></table></figure>


<p>如果 镜像被容器在使用，默认是不能删除的，可以通过 -f 强制删除</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker rmi hello-world</span><br></pre></td></tr></table></figure>


<h1 id="四、容器Container"><a href="#四、容器Container" class="headerlink" title="四、容器Container"></a>四、容器Container</h1><h2 id="4-1-基础命令"><a href="#4-1-基础命令" class="headerlink" title="4.1 基础命令"></a>4.1 基础命令</h2><h3 id="1、运行容器"><a href="#1、运行容器" class="headerlink" title="1、运行容器"></a>1、运行容器</h3><p>在本地 如果有镜像 就可以运行该镜像 生成容器，如果镜像不存在 会去网上pull后在运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">docker run 镜像名</span><br><span class="line"></span><br><span class="line">--name  在启动的时候 给容器 起一个名字</span><br><span class="line"></span><br><span class="line">--interactive , -i		交互</span><br><span class="line">--tty , -t      终端</span><br><span class="line">这两个一般都是一起用 -it</span><br><span class="line"></span><br><span class="line">-d 执行的容器 不会直接开启交互</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker  run --name  myhello hello-world</span><br></pre></td></tr></table></figure>
<p>交互式启动容器</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name mycentos -it centos:7 </span><br></pre></td></tr></table></figure>




<h3 id="2、查看容器列表"><a href="#2、查看容器列表" class="headerlink" title="2、查看容器列表"></a>2、查看容器列表</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker ps  查看 所有活跃的容器</span><br><span class="line"></span><br><span class="line">docker ps -a  查看所有容器 包括停止的</span><br></pre></td></tr></table></figure>


<h3 id="3、从容器中提取出镜像"><a href="#3、从容器中提取出镜像" class="headerlink" title="3、从容器中提取出镜像"></a>3、从容器中提取出镜像</h3><p>Create a new image from a container’s changes</p>
<p>如果我们给现有的容器做了修改，而且这个修改以后还要常用，我们就可以提前出镜像，作为一个新的版本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]</span><br><span class="line"></span><br><span class="line"># 案例</span><br><span class="line">docker  commit -a &quot;刘某人&quot; -m &quot;看你不爽,修改一下&quot;  d62332d7bf22  mycentos:7.1</span><br></pre></td></tr></table></figure>
<p>查看本地镜像列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost jiyun]# docker images</span><br><span class="line">REPOSITORY                 TAG            IMAGE ID       CREATED          SIZE</span><br><span class="line">mycentos                   7.1            c415d57cbff4   15 seconds ago   227MB</span><br><span class="line">&lt;none&gt;                     &lt;none&gt;         9924749400a6   13 hours ago     314MB</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="4、镜像推送-仓库"><a href="#4、镜像推送-仓库" class="headerlink" title="4、镜像推送 仓库"></a>4、镜像推送 仓库</h3><p>1、给镜像打包tag</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]</span><br><span class="line"></span><br><span class="line"># 案例</span><br><span class="line"></span><br><span class="line">docker tag mycentos:7.1 xiaozhuanjia&#x2F;mycentos:7.1</span><br></pre></td></tr></table></figure>
<p>2、推送</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker push [OPTIONS] NAME[:TAG]</span><br><span class="line"></span><br><span class="line"># 案例</span><br><span class="line">docker push xiaozhuanjia&#x2F;mycentos:7.1</span><br></pre></td></tr></table></figure>




<h3 id="5、启动常见服务容器"><a href="#5、启动常见服务容器" class="headerlink" title="5、启动常见服务容器"></a>5、启动常见服务容器</h3><p>如果想要容器 长时间运行 作为服务，需要加-d 后台运行 </p>
<p>-d执行的容器 不会直接开启交互</p>
<p>我们需要使用 docker exec 进入到容器</p>
<p>docker exec -it 1e592c76445b /bin/bash</p>
<h4 id="5-1-启动redis"><a href="#5-1-启动redis" class="headerlink" title="5.1  启动redis"></a>5.1  启动redis</h4><p>-p   端口号的映射    宿主机端口:容器端口</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">docker run -itd -p 6379:6379 redis   </span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 还可以继续开 开多个</span></span><br><span class="line">docker run -itd -p 6378:6379 redis</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> redis启动进入容器</span></span><br><span class="line">redis-cli -h 127.0.0.1 -p 6378</span><br></pre></td></tr></table></figure>


<h4 id="5-2-开启mysql"><a href="#5-2-开启mysql" class="headerlink" title="5.2 开启mysql"></a>5.2 开启mysql</h4><p>和普通开发服务一样，但也有特定， 需要知道 账号 密码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name mysql57 -itd  -p 3305:3306 -e MYSQL_ROOT_PASSWORD&#x3D;Guo_1234 mysql:5.7</span><br></pre></td></tr></table></figure>


<p>进入到mysql容器服务里</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker exec -it 容器id /bin/bash</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 之后执行用户登录就能成功进入mysql进行操作了</span></span><br><span class="line">mysql -u root -p</span><br><span class="line">enterpassword：你的密码</span><br></pre></td></tr></table></figure>
<p>然后可以操作数据库，进行数据库的增删改查操作</p>
<h3 id="6、Dockerfile使用"><a href="#6、Dockerfile使用" class="headerlink" title="6、Dockerfile使用"></a>6、Dockerfile使用</h3><p>如果我们想从零开始构建镜像，例如我们把我们的django项目 或者vue项目 作为一个镜像供其他人使用</p>
<p>可以使用Dockerfile文件来 描述这个镜像的构建过程。</p>
<h4 id="6-1-基础命令"><a href="#6-1-基础命令" class="headerlink" title="6.1 基础命令"></a>6.1 基础命令</h4><p><strong>FROM</strong>  定制的镜像都是基于 FROM 的镜像</p>
<p><strong>RUN</strong>：用于执行后面跟着的命令行命令   在执行 docker build 会执行该命令</p>
<p><strong>COPY</strong>: 拷贝复制文件</p>
<p><strong>CMD</strong> :  在docker run的时候会 执行 这里的命令</p>
<h4 id="6-2-给django项目写文件"><a href="#6-2-给django项目写文件" class="headerlink" title="6.2 给django项目写文件"></a>6.2 给django项目写文件</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 生成一个django项目的镜像</span><br><span class="line">FROM 3.6-alpine</span><br><span class="line">COPY requirement.txt  .</span><br><span class="line">RUN  pip3 install -r requirement.txt</span><br><span class="line">COPY hellopro .</span><br><span class="line">CMD [&quot;python3&quot;,&quot;manage.py&quot;,&quot;runserver&quot;]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>完成后使用docker build来生成镜像</strong></p>
<p>在 Dockerfile 文件的存放目录下，执行构建动作。</p>
<p>以下示例，通过目录下的 Dockerfile 构建一个 django/v1（镜像名称:镜像标签)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t django/v1 .</span><br></pre></td></tr></table></figure>

















      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/04/23/docker%E7%9A%84%E4%BD%BF%E7%94%A8/" data-id="cknucg22a000058uh8rnn7e5u" data-title="docker的使用" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          nginx部署flask项目
        
      </div>
    </a>
  
  
    <a href="/blog1/2021/04/22/CentOS%E9%83%A8%E7%BD%B2django%E9%A1%B9%E7%9B%AE/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">nginx部署django项目</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/Flask/" rel="tag">Flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/django/" rel="tag">django</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" rel="tag">关于第三方</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog1/tags/CentOS/" style="font-size: 17.5px;">CentOS</a> <a href="/blog1/tags/Flask/" style="font-size: 10px;">Flask</a> <a href="/blog1/tags/django/" style="font-size: 12.5px;">django</a> <a href="/blog1/tags/docker/" style="font-size: 12.5px;">docker</a> <a href="/blog1/tags/gitee/" style="font-size: 10px;">gitee</a> <a href="/blog1/tags/linux/" style="font-size: 10px;">linux</a> <a href="/blog1/tags/mysql/" style="font-size: 12.5px;">mysql</a> <a href="/blog1/tags/python/" style="font-size: 15px;">python</a> <a href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 17.5px;">python基础知识</a> <a href="/blog1/tags/redis/" style="font-size: 12.5px;">redis</a> <a href="/blog1/tags/vue/" style="font-size: 20px;">vue</a> <a href="/blog1/tags/webshell/" style="font-size: 10px;">webshell</a> <a href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" style="font-size: 10px;">云主机实现视频直播</a> <a href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" style="font-size: 12.5px;">关于第三方</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/01/">一月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog1/2021/05/14/docker+CentOS+mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">docker+CentOS+mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/13/mysql%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB(%E5%9C%A8%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E5%9F%BA%E7%A1%80%E4%B8%8A)/">mysql实现读写分离</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/">在项目中自动生成接口文档</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/">nginx部署flask项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Jerry<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog1/" class="mobile-nav-link">Home</a>
  
    <a href="/blog1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog1/js/jquery-3.4.1.min.js"></script>



  
<script src="/blog1/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog1/js/script.js"></script>





  </div>
</body>
</html>